package com.hsmw.api.controller.driver;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hsmw.api.mapper.HsmwScoringRecordMapper;
import com.hsmw.api.service.*;
import com.hsmw.api.vo.DriverClassVo;
import com.htn.common.core.base.DataResponse;
import com.htn.common.core.constant.CommonConstants;
import com.hsmw.api.utils.CommonUtils;
import com.htn.common.core.utils.StringUtils;
import com.hsmw.domain.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import com.hsmw.common.base.BaseController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 司机学习首页数据 前端控制器
 * </p>
 *
 * @author wd
 * @since 2021-09-06
 */
@Api(tags = "学习首页")
@RestController
@RequestMapping("/hsmw/home")
public class HsmwDriverClassHomeController extends BaseController<HsmwDriverClassHomeService, HsmwDriverClassHome> {


    @Autowired
    private HsmwDriverClassHomeService hsmwDriverClassHomeService;

    @Autowired
    private HsmwDriverClassService hsmwDriverClassService;

    @Autowired
    private HsmwDriverClassRecordService hsmwDriverClassRecordService;

    @Autowired
    private HsmwDriverClassTitleService driverClassTitleService;

    @Autowired
    private HsmwDriverClassVideoService hsmwDriverClassVideoService;

    @Autowired
    private HsmwDriverClassTitleRecordService hsmwDriverClassTitleRecordService;

    @Autowired
    private HsmwUserDriverService hsmwUserDriverService;

    @Autowired
    private HsmwScoringRecordMapper hsmwScoringRecordMapper;


    @ApiOperation(value = "查询首页信息", notes = "查询首页信息", produces = "application/json")
    @ApiImplicitParam(name = "id", value = "司机id", dataType = "String")
    @PostMapping("/findDriverClassHomeList")
    public DataResponse findDriverClassHomeList(String id) {


        List<HsmwDriverClass> driverClasses = hsmwDriverClassService.list(new LambdaQueryWrapper<HsmwDriverClass>().eq(HsmwDriverClass::getDriverId, id).orderByAsc(HsmwDriverClass::getHomeId));
        if (CollectionUtil.isNotEmpty(driverClasses)) {
            return new DataResponse(driverClasses);
        } else {
            List<HsmwDriverClass> hsmwDriverClasses = null;
            List<HsmwDriverClassHome> driverClassHomes = hsmwDriverClassHomeService.list(new LambdaQueryWrapper<HsmwDriverClassHome>());
            driverClassHomes.stream().forEach(driverClassHome -> {
                HsmwDriverClass hsmwDriverClass = new HsmwDriverClass();
                hsmwDriverClass.setDriverId(id);
                hsmwDriverClass.setHomeId(driverClassHome.getId());
                hsmwDriverClass.setHomeName(driverClassHome.getTitle());
                hsmwDriverClass.setHomeSubtitle(driverClassHome.getSubtitle());
                hsmwDriverClass.setProgress("0");
                hsmwDriverClass.setImages(driverClassHome.getImages());
                hsmwDriverClassService.save(hsmwDriverClass);
            });
            hsmwDriverClasses = hsmwDriverClassService.list(new LambdaQueryWrapper<HsmwDriverClass>().eq(HsmwDriverClass::getDriverId, id).orderByAsc(HsmwDriverClass::getHomeId));
            return new DataResponse(hsmwDriverClasses);
        }
    }


    @ApiOperation(value = "点击去学习", notes = "点击去学习", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "司机id", dataType = "String"),
            @ApiImplicitParam(name = "homeId", value = "首页的id", dataType = "String")
    })
    @PostMapping("/findDriverClassDetails")
    public DataResponse findDriverClassDetails(String id, String homeId) {
        List<HsmwDriverClassTitleRecord> driverClassRecordListInfo = hsmwDriverClassTitleRecordService.findDriverClassRecordListInfo(id, homeId);
        if (CollectionUtil.isNotEmpty(driverClassRecordListInfo)) {
            return new DataResponse(driverClassRecordListInfo);
        } else {

            List<HsmwDriverClassTitleRecord> driverClassTitleRecords = null;
            // 根据homeId 查询子列表 根据子列表id查询 视频
            List<HsmwDriverClassTitle> driverClassTitles = driverClassTitleService.findTitlelistByHomeId(homeId);
            List<HsmwDriverClassRecord> records = new ArrayList<>();
            List<HsmwDriverClassTitleRecord> titleRecords = new ArrayList<>();
            driverClassTitles.stream().forEach(hsmwDriverClassTitle -> {
                HsmwDriverClassTitleRecord hsmwDriverClassTitleRecord = new HsmwDriverClassTitleRecord();
                hsmwDriverClassTitleRecord.setTitleId(hsmwDriverClassTitle.getTitleId());
                hsmwDriverClassTitleRecord.setTitleName(hsmwDriverClassTitle.getTitleName());
                hsmwDriverClassTitleRecord.setHomeId(homeId);
                hsmwDriverClassTitleRecord.setDriverId(id);
                titleRecords.add(hsmwDriverClassTitleRecord);


                List<HsmwDriverClassVideo> driverClassVideos = hsmwDriverClassTitle.getDriverClassVideos();
                driverClassVideos.stream().forEach(hsmwDriverClassVideo -> {
                    HsmwDriverClassRecord hsmwDriverClassRecord = new HsmwDriverClassRecord();
                    hsmwDriverClassRecord.setDriverId(id);
                    hsmwDriverClassRecord.setHomeId(homeId);
                    hsmwDriverClassRecord.setTitleId(hsmwDriverClassTitle.getTitleId());
                    hsmwDriverClassRecord.setVideoDuration(hsmwDriverClassVideo.getVideoDuration());
                    hsmwDriverClassRecord.setVideoName(hsmwDriverClassVideo.getVideoName());
                    hsmwDriverClassRecord.setVideoPeopleNumber("10");
                    hsmwDriverClassRecord.setVideoInfo(hsmwDriverClassVideo.getVideoInfo());
                    hsmwDriverClassRecord.setVideoProgress("0");
                    hsmwDriverClassRecord.setVideoId(hsmwDriverClassVideo.getVideoId());
                    hsmwDriverClassRecord.setId(CommonUtils.generateId(hsmwDriverClassRecord));
                    records.add(hsmwDriverClassRecord);
                });


            });
            // 新增子标题学习表
            hsmwDriverClassTitleRecordService.saveBatch(titleRecords);
            // 新增 学习记录表
            hsmwDriverClassRecordService.saveBatch(records);

            driverClassTitleRecords = hsmwDriverClassTitleRecordService.findDriverClassRecordListInfo(id, homeId);
            return new DataResponse(driverClassTitleRecords);
        }

    }


    @ApiOperation(value = "更新进度 学习人数 总进度", notes = "更新进度 学习人数 总进度", produces = "application/json")
    @PostMapping("/updateDriverClassInfo")
    public DataResponse updateDriverClassInfo(@RequestBody DriverClassVo driverClassVo) {

        if (driverClassVo == null) {
            return new DataResponse(CommonConstants.ResponseStatus.FAIL, "传递参数出现错误");
        }
        HsmwDriverClass home = hsmwDriverClassService.getOne(new LambdaQueryWrapper<HsmwDriverClass>().eq(HsmwDriverClass::getDriverId,driverClassVo.getDriverId()).eq(HsmwDriverClass::getHomeId,driverClassVo.getHomeId()));
        if(!StringUtils.equals("100",home.getProgress())){
            home.setProgress(driverClassVo.getProgress());
        }

        boolean h = hsmwDriverClassService.updateById(home);

        HsmwDriverClassRecord video = hsmwDriverClassRecordService.getOne(new LambdaQueryWrapper<HsmwDriverClassRecord>().eq(HsmwDriverClassRecord::getVideoId,driverClassVo.getVideoId()).eq(HsmwDriverClassRecord::getDriverId,driverClassVo.getDriverId()));
        if(!StringUtils.equals("100",video.getVideoProgress())){
            video.setVideoProgress(driverClassVo.getVideoProgress());
            video.setVideoPeopleNumber(String.valueOf(Long.parseLong(video.getVideoPeopleNumber())+1));
        }
        boolean v = hsmwDriverClassRecordService.updateById(video);

        List<HsmwDriverClass> list = hsmwDriverClassService.list(new LambdaQueryWrapper<HsmwDriverClass>().eq(HsmwDriverClass::getDriverId, driverClassVo.getDriverId()).eq(HsmwDriverClass::getFlag,"0"));
        boolean flag = true;
        if(CollectionUtil.isNotEmpty(list)){
            for (HsmwDriverClass hsmwDriverClass : list) {
                if(!StringUtils.equals("100",hsmwDriverClass.getProgress())){
                    flag = false;
                    break;
                }else {
                    hsmwDriverClass.setFlag("1");
                }
            }
        }else {
            flag = false;
        }
        hsmwDriverClassService.updateBatchById(list);
        if(flag){
            HsmwUserDriver userDriver = hsmwUserDriverService.getById(driverClassVo.getDriverId());
            HsmwScoringRecord scoringRecord = new HsmwScoringRecord();
            scoringRecord.setDriverId(userDriver.getId());
            scoringRecord.setInitialScore(userDriver.getScore());
            scoringRecord.setEndScore(String.valueOf(Long.parseLong(userDriver.getScore()) + 20));
            scoringRecord.setFluctuateScore("20");
            scoringRecord.setRemark("学习完成分数");
            hsmwScoringRecordMapper.insert(scoringRecord);
            userDriver.setScore(String.valueOf(Long.parseLong(userDriver.getScore()) + 20));
            hsmwUserDriverService.updateById(userDriver);
        }

        if (h && v) {
            return new DataResponse(CommonConstants.ResponseStatus.SUCCESS);
        }
        return new DataResponse(CommonConstants.ResponseStatus.FAIL);
    }


}


